package com.filldream.fastboot.common.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.filldream.fastboot.common.entity.ErrorCode;
import com.filldream.fastboot.common.entity.R;
import com.filldream.fastboot.common.util.entity.express.JuHeExpress;
import com.filldream.fastboot.common.util.entity.express.JuHeExpressList;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author RickSun
 * @Description 聚合-快递信息查询
 **/
@Slf4j
public class JuHeExpUtil {

    //聚合-查询快递信息
    private static final String expUrl = "http://v.juhe.cn/exp/index";

    //聚合-查询快递公司名缩写
    private static final String comUrl = "http://v.juhe.cn/exp/com";

    //聚合Key
//    private static final String key = "bcdb4a3c93b959e75fb907db492ea777";

    /**
     * 获取聚合快递信息
     * @param com 快递公司编号
     * @param no 快递单号
     * @param key 聚合key
     * @return "R<JuHeExpress>" 统一返回类
     */
    public static R<JuHeExpress> getExp(String com, String no, String key) {
        JuHeExpress juHeExpress = null;
        try {
            juHeExpress = getExpList(com, no, key);
        } catch (IOException e) {
            e.printStackTrace();
            return R.createError(ErrorCode.BASE_ERR.reMsg("快递请求异常(IO)"));
        }
        if( juHeExpress.getErrorCode().equals(0) && juHeExpress.getResultcode().equals(200) ){
            return R.diy(ErrorCode.SUCCESS.reMsg(),juHeExpress,juHeExpress.getReason());
        }
        return R.createError(ErrorCode.BASE_ERR.reMsg(juHeExpress.getReason()),false);
    }

    /**
     * 获取聚合快递信息
     * @param com 快递公司编号
     * @param no 快递单号
     * @param key 聚合key
     * @throws IOException
     * return 返回实体类
     */
    public static JuHeExpress getExpList(String com, String no, String key) throws IOException {
        if(StringUtils.isBlank(com)){com = "auto";}
        Assert.isEmpty(no,"快递单号不能位空");
        String url = expUrl + "?com=" + com + "&no=" + no + "&dtype=json&key=" + key;;
        String juheJson = UrlUtil.doGet(url);
        JuHeExpress result = JsonUtil.fromJson(juheJson, JuHeExpress.class);
        JSONObject jsonObject = JSONObject.parseObject(juheJson);
        Map<String, Object> map = (Map<String, Object>) jsonObject.get("result");
        result.setCom(com);
        result.setNo(no);
        result.setExpressList(Collections.emptyList());
        if(map != null && !map.isEmpty()){
            result.setCompany((String)map.get("company"));
            result.setCom((String)map.get("com"));
            result.setNo((String)map.get("no"));
            result.setStatus((String)map.get("status"));
            result.setStatusDetail((String)map.get("status_detail"));
            JSONArray list = (JSONArray)map.get("list");
            List<JuHeExpressList> juHeExpressLists = JsonUtil.fromJosnArray(list.toJSONString(), JuHeExpressList.class);
            if(!juHeExpressLists.isEmpty()){
                //按时间降序
                List<JuHeExpressList> sortList = ListUtil.sortByBean(juHeExpressLists, "datetime", false);
                result.setExpressList(sortList);
            }
        }
        return result;
    }

    /**
     * @author RickSun
     * @Description 获取快递公司缩写
     * @param key 聚合Key
     **/
    public static String getComList( String key) throws IOException {
        String url = comUrl + "?key=" + key;
        String juheJson = UrlUtil.doGet(url);
        return juheJson;
    }

}